
#import "KVPair.h"

#define NoNULL(string) ((string==NULL)?"":string)


@implementation KVPair

//-----------------------------------------------------------
// IDENTIFIERS
//-----------------------------------------------------------

+ (NXAtom)keyIdentifier;
	{
	return NXUniqueString("KVKeyIdentifier");
	}
	

+ (NXAtom)valueIdentifier;
	{
	return NXUniqueString("KVValueIdentifier");
	}


//-----------------------------------------------------------
// INIT & FREE
//-----------------------------------------------------------

- init:(const char *)kv delimiter:(char)del;
	{
	char	*buf,*p;
	
	[self init];
	buf=NXCopyStringBufferFromZone(kv,[self zone]);
	if(p=strchr(buf,(int)del))
		{
		*p='\0'; p++;
		[self setKey:buf];
		[self setValue:p];
		}
	
	NXZoneFree([self zone],buf);
	return self;
	}


- initKey:(const char *)aKey value:(const char *)aValue;
	{
	[self init];
	[self setKey:aKey];
	[self setValue:aValue];
	return self;
	}


- init;
	{
	[super init];
	key=[[MiscString allocFromZone:[self zone]] init];
	value=[[MiscString allocFromZone:[self zone]] init];
	return self;
	}
	
	
- free
	{
	key=[key free];
	value=[value free];
	return [super free];
	}
	
	
//-----------------------------------------------------------
// COPY 
//-----------------------------------------------------------
	
- copyFromZone:(NXZone *)zone;
	{
	KVPair	*new=[super copyFromZone:zone];
	
	new->key=[key copyFromZone:zone];
	new->value=[value copyFromZone:zone];
	return new;
	}
	
	
//-----------------------------------------------------------
// KEY 
//-----------------------------------------------------------

- setKey:(const char *)string;
	{
	[key setStringValue:string];
	[key trimWhiteSpaces];
	return self;
	}

- takeKeyFrom:anObject
	{
	return [self setKey:[anObject stringValue]];
	}
	
- (const char *)key;
	{
	const char	*p=[key stringValue];
	
	return NoNULL(p);
	}
	

//-----------------------------------------------------------
// VALUE 
//-----------------------------------------------------------

- setValue:(const char *)string;
	{
	[value setStringValue:string];
	[value trimWhiteSpaces];
	return self;
	}

- takeValueFrom:anObject
	{
	return [self setValue:[anObject stringValue]];
	}
	
- (const char *)value;
	{
	const char	*p=[value stringValue];
	
	return NoNULL(p);
	}


//-----------------------------------------------------------
//	MiscTCRow Protocol
//-----------------------------------------------------------

- (void *)insertKey:(const void *)aKey value:(void *)aValue;
	{
	if(aKey==[KVPair keyIdentifier])
		[self setKey:aValue];
	else if(aKey==[KVPair valueIdentifier])
		[self setValue:aValue];
	return self;
	}


- (void *)valueForKey:(const void *)aKey;
	{
	const void *ret=NULL;

	if(aKey==[KVPair keyIdentifier])
		ret=[self key];
	else if(aKey==[KVPair valueIdentifier])
		ret=[self value];
	return (void *)ret;
	}


//-----------------------------------------------------------
// TEST WHETHER EMPTY 
//-----------------------------------------------------------

- (BOOL)isNull;
	{
	return [key emptyString];
	}


- (BOOL)hasValue;
	{
	return (![key emptyString] && ![value emptyString]);
	}
	

//-----------------------------------------------------------
// THAT'S IT
//-----------------------------------------------------------

@end
